#ifndef USERMANAGER_H
#define USERMANAGER_H

#include <QObject>

class UserManager : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged FINAL)
    Q_PROPERTY(int level READ level WRITE setLevel NOTIFY levelChanged FINAL)  // 修正了拼写错误：lever -> level
    Q_PROPERTY(int availablePoints READ availablePoints WRITE setAvailablePoints NOTIFY availablePointsChanged FINAL)
    Q_PROPERTY(int todayPoints READ todayPoints WRITE setTodayPoints NOTIFY todayPointsChanged FINAL)
    Q_PROPERTY(int totalPoints READ totalPoints WRITE setTotalPoints NOTIFY totalPointsChanged FINAL)

public:
    explicit UserManager(QObject *parent = nullptr);

    // Getter 函数
    QString name() const { return m_name; }
    int level() const { return m_level; }
    int availablePoints() const { return m_availablePoints; }
    int todayPoints() const { return m_todayPoints; }
    int totalPoints() const { return m_totalPoints; }

    // Setter 函数
    void setName(const QString &name) {
        if (m_name != name) {
            m_name = name;
            emit nameChanged();
        }
    }

    void setLevel(int level) {
        if (m_level != level) {
            m_level = level;
            emit levelChanged();
        }
    }

    void setAvailablePoints(int points) {
        if (m_availablePoints != points) {
            m_availablePoints = points;
            emit availablePointsChanged();
        }
    }

    void setTodayPoints(int points) {
        if (m_todayPoints != points) {
            m_todayPoints = points;
            emit todayPointsChanged();
        }
    }

    void setTotalPoints(int points) {
        if (m_totalPoints != points) {
            m_totalPoints = points;
            emit totalPointsChanged();
        }
    }

    // 积分增减修改函数
    Q_INVOKABLE void pointsIncrease(int points);
    Q_INVOKABLE void pointsDecrease(int points);
    // 在UserManager类中添加
    Q_INVOKABLE bool canExchangeReward(int points) const {
        return m_availablePoints >= points;
    }

    // 保存数据到文件
    Q_INVOKABLE void saveUserData();

    // 从文件加载数据
    Q_INVOKABLE void loadUserData();

signals:
    void nameChanged();
    void levelChanged();
    void availablePointsChanged();
    void todayPointsChanged();
    void totalPointsChanged();

private:
    QString m_name;
    int m_level;
    int m_availablePoints;
    int m_todayPoints;
    int m_totalPoints;

    // 初始化默认数据
    void initializeDefaultData();

    // 获取数据文件路径
    QString getDataFilePath() const;

    // 创建初始 JSON 文件
    void createInitialJsonFile();

    // 从 JSON 文件加载数据
    bool loadFromJsonFile();
};

#endif // USERMANAGER_H
